<?php
namespace Agent\Controller;
use Think\Controller;
class ImportController extends BaseController {
	public function _initialize()
    {
        parent::_initialize();
        $g=session('agent');
        $db_name=$g['db'];
		C('erp_dbname',$g['db']);
        $this->product = M('product',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
		//数据库连接基础配置-begin
		C('db_config.DB_TYPE',C('DB_TYPE'));
		C('db_config.DB_HOST',C('DB_HOST'));
		C('db_config.DB_USER',C('DB_USER'));
		C('db_config.DB_PWD',C('DB_PWD'));
		C('db_config.DB_PORT',C('DB_PORT'));
		C('db_config.DB_CHARSET',C('DB_CHARSET'));
		C('db_config.DB_NAME',C('erp_dbname'));
		//数据库连接基础配置-end
    }
	public function importInboundplan(){
        $this->display();
    }
	//导入计划入库单
	function doimportInboundplan(){
		$g=session('agent');
        $db_name=$g['db'];
		$trans=M('', C('DB_PREFIX_T'), C('db_config'));
		$trans->startTrans();   // 开启事务
		$m=M('InboundPlanDetail',C('DB_PREFIX_T'), C('db_config'));
		$p=M('Product',C('DB_PREFIX_T'), C('db_config'));
		$u=M('Unit');
		$list_id=I('id');
		if (!empty($_FILES)){
			$config = array(
				'maxSize'    =>    3145728,    
				'savePath'   =>    '/Import/',    
				'saveName'   =>    array('uniqid',''),    
				'exts'       =>    array('xlsx', 'xls'),    
				'autoSub'    =>    true,    
				'subName'    =>    array('date','Ymd'),);
			$upload = new \Think\Upload($config);// 实例化上传类
			$info   =   $upload->upload();    
			if(!$info) {// 上传错误提示错误信息
				$return['status']=0;
				$return['message']=$upload->getError();
				$this->ajaxReturn($return);   
			}
			$extension=$info['import']['ext'];
			// vendor("PHPExcel.PHPExcel");
			//导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
			import("Org.Util.PHPExcel");
			import("Org.Util.PHPExcel.Writer.Excel5");
			import("Org.Util.PHPExcel.Writer.Excel2007");
			import("Org.Util.PHPExcel.IOFactory.php");
			
			//创建PHPExcel对象，注意，不能少了\
			$objPHPExcel = new \PHPExcel();
			
			$file_name='./Uploads'.$info['import']['savepath'].$info['import']['savename'];
			// dump($file_name);
			// die;
			if($extension=='xlsx'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
			}
			if($extension=='xls'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel5');
			}
			$objPHPExcel = $objReader->load($file_name,$encode='utf-8');
			$sheet = $objPHPExcel->getSheet(0);
			$highestRow = $sheet->getHighestRow(); // 取得总行数
			$highestColumn = $sheet->getHighestColumn(); // 取得总列数

			for($i=2;$i<=$highestRow;$i++)
			{
				$data=array();
				$goods_no= $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();//商品编码
				$p_info	=	$p->where(array('goods_no'=>$goods_no))->find();
				if(empty($p_info)){
					$trans->rollback();
					$return['status']=0;
					$return['message']='第'.$i.'行的商品编码不存在';
					$this->ajaxReturn($return);
				}else{
					$data['product_id']=$p_info['id'];
				}
				
				$data['warehouse_code']		= (string)$objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
				$data['warehouse_name']		= (string)$objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
				$data['price']		= $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
				$data['count']		= $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				$data['unit_no']		= (string)$objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
				$data['unit_name']	= $u->where(array('unit_no'=>$data['unit_no']))->getField('unit_name');
				$data['expected_time']			= $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();//预计到货时间
				$data['unit_t_no']		= (string)$objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
				$data['unit_t_count']			= $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
				$data['unit_t_name']	= $u->where(array('unit_no'=>$data['unit_t_no']))->getField('unit_name');
				$data['other_count']		= $objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();
				
				$data['list_id']=$list_id;
				$data['is_shelves']=0;
				$data['is_back']=0;
				$data['is_delete']=0;
				$data['is_push']=0;
				$massage=session('agent');
				$data['operator']=$massage['username'];
				$data['created_at']=date("Y-m-d H:i:s",time());
				$data['is_examined']=0;
				//添加
				$res=$m->add($data);
				if(!$res){
					$trans->rollback();
				}
			}
			$trans->commit();
			$return['status']=1;
			$return['message']='导入成功！';
			$this->ajaxReturn($return);
		}else
		{
			$return['status']=0;
			$return['message']='请选择上传的文件';
			$this->ajaxReturn($return);
		}
	}
	public function importListproduct(){
        $this->display();
    }
	//导入采购清单
	function doimportListproduct(){
		$g=session('agent');
        $db_name=$g['db'];
		$trans=M('', C('DB_PREFIX_T'), C('db_config'));
		$trans->startTrans();   // 开启事务
		$m=M('ListProduct',C('DB_PREFIX_T'), C('db_config'));
		$p=M('Product',C('DB_PREFIX_T'), C('db_config'));
		$u=M('Unit');
		$list_id=I('id');
		if (!empty($_FILES)){
			$config = array(
				'maxSize'    =>    3145728,    
				'savePath'   =>    '/Import/',    
				'saveName'   =>    array('uniqid',''),    
				'exts'       =>    array('xlsx', 'xls'),    
				'autoSub'    =>    true,    
				'subName'    =>    array('date','Ymd'),);
			$upload = new \Think\Upload($config);// 实例化上传类
			$info   =   $upload->upload();    
			if(!$info) {// 上传错误提示错误信息
				$return['status']=0;
				$return['message']=$upload->getError();
				$this->ajaxReturn($return);   
			}
			$extension=$info['import']['ext'];
			// vendor("PHPExcel.PHPExcel");
			//导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
			import("Org.Util.PHPExcel");
			import("Org.Util.PHPExcel.Writer.Excel5");
			import("Org.Util.PHPExcel.Writer.Excel2007");
			import("Org.Util.PHPExcel.IOFactory.php");
			
			//创建PHPExcel对象，注意，不能少了\
			$objPHPExcel = new \PHPExcel();
			
			$file_name='./Uploads'.$info['import']['savepath'].$info['import']['savename'];
			// dump($file_name);
			// die;
			if($extension=='xlsx'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
			}
			if($extension=='xls'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel5');
			}
			$objPHPExcel = $objReader->load($file_name,$encode='utf-8');
			$sheet = $objPHPExcel->getSheet(0);
			$highestRow = $sheet->getHighestRow(); // 取得总行数
			$highestColumn = $sheet->getHighestColumn(); // 取得总列数

			for($i=2;$i<=$highestRow;$i++)
			{
				$data=array();
				$goods_no= $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();//商品编码
				$p_info	=	$p->where(array('goods_no'=>$goods_no))->find();
				if(empty($p_info)){
					$trans->rollback();
					$return['status']=0;
					$return['message']='第'.$i.'行的商品编码不存在';
					$this->ajaxReturn($return);
				}else{
					$data['product_id']=$p_info['id'];
				}
				$data['warehouse_code']		= (string)$objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
				$data['warehouse_name']		= (string)$objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
				$data['price']		= $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
				$data['count']		= $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				$data['unit_no']		= (string)$objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
				$data['unit_name']	= $u->where(array('unit_no'=>$data['unit_no']))->getField('unit_name');
				$data['expected_time']			= $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();//预计到货时间
				$data['unit_t_no']		= (string)$objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
				$data['unit_t_count']			= $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
				$data['unit_t_name']	= $u->where(array('unit_no'=>$data['unit_t_no']))->getField('unit_name');
				$data['other_count']		= $objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();
				
				$data['list_id']=$list_id;
				$data['is_shelves']=0;
				$data['is_back']=0;
				$data['is_delete']=0;
				$data['is_push']=0;
				$massage=session('agent');
				$data['operator']=$massage['username'];
				$data['created_at']=date("Y-m-d H:i:s",time());
				$data['is_examined']=0;
				//添加
				$res=$m->add($data);
				if(!$res){
					$trans->rollback();
				}
			}
			$trans->commit();
			$return['status']=1;
			$return['message']='导入成功！';
			$this->ajaxReturn($return);
		}else
		{
			$return['status']=0;
			$return['message']='请选择上传的文件';
			$this->ajaxReturn($return);
		}
	}
	public function importProduct(){
        $this->display();
    }
	public function importPushorder(){
		$download_id="3-agent.xls";//永卓分销模板
		
		$this->assign('download_id',$download_id);
        $this->display();
    }
	// public function importPushorder(){
		// $param = I('param.','','trim');
		// $All=$param['All'];
		// $warehouse_code=$param['warehouse_code'];//当前仓库编码
		
		// $warehouse=M('Warehouse');//仓库表
		// $warehouse_info=$warehouse->where(array('warehouse_code'=>$warehouse_code))->find();
		
		// if($warehouse_info['warehouse_cate']==1){//一般仓
			// $importExcelPushorderTempName_Normal=1;//配置订单模板(一般正常模板)
		// }
		// if($warehouse_info['warehouse_cate']==2){//跨境仓
			// $importExcelPushorderTempName=C('base_config.importExcelPushorderTempName');//配置跨境定制模板名称
		// }
		// if(empty($importExcelPushorderTempName)){
			// if(!empty($importExcelPushorderTempName_Normal)){
				// $download_id="3-normal.xls";//一般正常模板
			// }else{
				// $download_id="3-1.xls";//跨境正常模板
			// }
		// }else{
			// $download_id=$importExcelPushorderTempName;//跨境定制模板
		// }
		// if($All=='All' and $warehouse_info['warehouse_cate']==2){//跨境仓-导入全部数据
			// $download_id="3-1-All.xls";//跨境一般模板（全部）
		// }
		// if($All=='All' and $warehouse_info['warehouse_cate']==1){//一般仓-导入全部数据
			// $download_id="3-normal-All.xls";//一般正常模板（全部）
		// }
		// dump($All);
		// $this->assign('importExcelPushorderTempName',$importExcelPushorderTempName);
		// $this->assign('download_id',$download_id);
        // $this->display();
    // }
	//导入产品
	function doimportProduct(){
		$g=session('agent');
        $db_name=$g['db'];
		$trans=M('', C('DB_PREFIX_T'), C('db_config'));
		$trans->startTrans();   // 开启事务
		$m=M('Product',C('DB_PREFIX_T'), C('db_config'));
		$b=M('Book',C('DB_PREFIX_T'), C('db_config'));
		
		if (!empty($_FILES)){
			$config = array(
				'maxSize'    =>    3145728,    
				'savePath'   =>    '/Import/',    
				'saveName'   =>    array('uniqid',''),    
				'exts'       =>    array('xlsx', 'xls'),    
				'autoSub'    =>    true,    
				'subName'    =>    array('date','Ymd'),);
			$upload = new \Think\Upload($config);// 实例化上传类
			$info   =   $upload->upload();    
			if(!$info) {// 上传错误提示错误信息 
				$return['status']=0;
				$return['message']=$upload->getError();
				$this->ajaxReturn($return);    
			}
			$extension=$info['import']['ext'];
			// vendor("PHPExcel.PHPExcel");
			//导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
			import("Org.Util.PHPExcel");
			import("Org.Util.PHPExcel.Writer.Excel5");
			import("Org.Util.PHPExcel.Writer.Excel2007");
			import("Org.Util.PHPExcel.IOFactory.php");
			
			//创建PHPExcel对象，注意，不能少了\
			$objPHPExcel = new \PHPExcel();
			
			$file_name='./Uploads'.$info['import']['savepath'].$info['import']['savename'];
			// dump($file_name);
			// die;
			if($extension=='xlsx'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
			}
			if($extension=='xls'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel5');
			}
			$objPHPExcel = $objReader->load($file_name,$encode='utf-8');
			$sheet = $objPHPExcel->getSheet(0);
			$highestRow = $sheet->getHighestRow(); // 取得总行数
			$highestColumn = $sheet->getHighestColumn(); // 取得总列数
			
			$warehouse_cate=session('agent_cate');
			$Product=A('Product');
			for($i=2;$i<=$highestRow;$i++)
			{
				$data=array();
				$data['name']		= (string)$objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
				$data['unit']		= (string)$objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
				$data['unit_no']		= (string)$objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
				$data['standard']		= (string)$objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
				$data['unit_t']		= (string)$objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				$data['unit_no_t']		= (string)$objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
				$data['unit_t_goods_spec']	= (string)$objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
				$data['sn']		= (string)$objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
				
		
				$data['pn']			= (string)$objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
				
				//料号校检
				
				if($warehouse_cate==2){//跨境仓
					$has_sourceno=$Product->has_sourceno($data['pn']);
					if($has_sourceno<>1){
						$has_sourceno=json_decode($has_sourceno,true);
						$trans->rollback();
						$return['status']=0;
						$return['message']=$has_sourceno['message'];
						$this->ajaxReturn($return);
					}
				}

				$data['coin_code']			= (string)$objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();
				$data['currency']			= (string)$objPHPExcel->getActiveSheet()->getCell("K".$i)->getValue();
				$data['country']			= (string)$objPHPExcel->getActiveSheet()->getCell("L".$i)->getValue();
				$data['country_code']		= (string)$objPHPExcel->getActiveSheet()->getCell("M".$i)->getValue();
				$data['zccode']				= (string)$objPHPExcel->getActiveSheet()->getCell("N".$i)->getValue();
				$data['category']			= (string)$objPHPExcel->getActiveSheet()->getCell("O".$i)->getValue();
				
				$data['hs_code']			= (string)$objPHPExcel->getActiveSheet()->getCell("P".$i)->getValue();
				$data['price']				= (string)$objPHPExcel->getActiveSheet()->getCell("Q".$i)->getCalculatedValue();//单价getCalculatedValue:计算公式后的值
				$data['item_type']			= (string)$objPHPExcel->getActiveSheet()->getCell("R".$i)->getValue();
				$data['item_no']			= (string)$objPHPExcel->getActiveSheet()->getCell("S".$i)->getValue();
				
				$data['net_weight']			= $objPHPExcel->getActiveSheet()->getCell("T".$i)->getCalculatedValue();
				$data['gross_weight']		= $objPHPExcel->getActiveSheet()->getCell("U".$i)->getCalculatedValue();
				
				$data['goods_tax']		= $objPHPExcel->getActiveSheet()->getCell("V".$i)->getCalculatedValue();
				
				$data['created_at']=date("Y-m-d H:i:s",time());
				$data['is_examined']=0;				
				
				$info=$b->where(array('book_no'=>$data['zccode']))->find();
				if(empty($info)){
					$trans->rollback();
					$return['status']=0;
					$return['message']='第'.$i.'行没有查到对应账册信息，请先添加对应账册信息或核对数据后再导入！';
					$this->ajaxReturn($return);
				}
				
				$merchant_code=$info['merchant_code'];
				$sn_length=strlen($sn);
				// if($sn_length<20){
					// $sn_new=$sn;
					// for($i=0;$i<20-$sn_length;$i++){
						// $sn_new=$sn_new.'0';
					// }
				// }else{
					// $sn_new=substr($sn,-20,20);
				// }
				$sn_new=$data['sn'];
				$data['goods_no']=$merchant_code.$sn_new;//商家编码（10位）+ 条形码（不足20位数字的在后面补0，超过20位的，截取后面的20位字符）
				//商家编码定义：公司--C+海关十位编码或者组织机构代码。如果有海关十位编码的优先考虑海关十位编码，没有海关十位编码的再考虑组织机构代码
				//注：组织机构代码不足十位的在后边补0
				//个人--P+手机号码。截取手机号码后十位
				//添加
				$dup=$m->where(array('goods_no'=>$data['goods_no']))->find();
				if(empty($data['sn'])){
					$trans->rollback();
					$return['status']=0;
					$return['message']='第'.$i.'行 条形码不能为空，请检查数据并删除空行';
					$this->ajaxReturn($return);
				}
				if(!empty($dup)){
					$trans->rollback();
					$return['status']=0;
					$return['message']='第'.$i.'行['.$data['sn'].']条形码重复';
					$this->ajaxReturn($return);
				}
				$res=$m->add($data);	
				// echo $m->getlastsql();
				if(!$res){
					$trans->rollback();
				}
			}
			$trans->commit();
			$return['status']=1;
			$return['message']='导入成功！';
			$this->ajaxReturn($return);
		}else
		{
			$return['status']=0;
			$return['message']='请选择上传的文件';
			$this->ajaxReturn($return);
		}
	}
	//导入订单-（一般正常模板）
	public function doimportPushorder_normal(){
		$param = I('param.','','trim');
		$owner_status=session('agent.owner_status');//货主属性0:否1是
		if(!$owner_status){//不是货主（主erp）
			$order=A('order');
			$order->is_owner($param['username'],$param['warehouse_code']);
		}
		$trans = M('',C('DB_PREFIX_T'), C('db_config'));
        $trans->startTrans();   // 开启事务
		$m=M('Pushorder',C('DB_PREFIX_T'), C('db_config'));
		$g=M('Pushordergoods',C('DB_PREFIX_T'), C('db_config'));
		$p=M('Printorder',C('DB_PREFIX_T'), C('db_config'));
		$pro=M('Product',C('DB_PREFIX_T'), C('db_config'));
		// $region=M('Region');
		//商家编码--begin
			$merchant_code=C('base_config.merchant_code');//商家编码
		//商家编码--end
		if (!empty($_FILES)) {
			$config = array(
				'maxSize'    =>    3145728,    
				'savePath'   =>    '/Import/',
				'saveName'   =>    array('uniqid',''),    
				'exts'       =>    array('xlsx', 'xls'),    
				'autoSub'    =>    true,    
				'subName'    =>    array('date','Ymd'),);
			$upload = new \Think\Upload($config);// 实例化上传类
			$info   =   $upload->upload();    
			if(!$info) {// 上传错误提示错误信息      
				$return['status']=0;
				$return['message']=$upload->getError();
				$this->ajaxReturn($return);    
			}
			$extension=$info['import']['ext'];
			// vendor("PHPExcel.PHPExcel");
			//导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
			import("Org.Util.PHPExcel");
			import("Org.Util.PHPExcel.Writer.Excel5");
			import("Org.Util.PHPExcel.Writer.Excel2007");
			import("Org.Util.PHPExcel.IOFactory.php");
			
			//创建PHPExcel对象，注意，不能少了\
			$objPHPExcel = new \PHPExcel();
			
			$file_name='./Uploads'.$info['import']['savepath'].$info['import']['savename'];
			// dump($file_name);
			// die;
			if($extension=='xlsx'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
			}
			if($extension=='xls'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel5');
			}
			$objPHPExcel = $objReader->load($file_name,$encode='utf-8');
			$sheet = $objPHPExcel->getSheet(0);
			$highestRow = $sheet->getHighestRow(); // 取得总行数
			$highestColumn = $sheet->getHighestColumn(); // 取得总列数
			// $sql = 'truncate table '.$db_name.'.'.C('DB_PREFIX_T').'arrival_order';
			// $stat=M()->execute($sql);//清空表
			$outbound=A('Outbound');
			$username=$param['username'];
			$warehouse_code=$param['warehouse_code'];
			$serialNumber_first=$outbound->serialNumber('Pushorder',$username,$warehouse_code);
			$order_foreachcount=0;
			for($i=2;$i<=$highestRow;$i++)
			{
				$data=array();
				$data_og=array();
				$print_order=array();
				$order_no		= $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();//订单编号
				// dump($order_no);dump($old_order_no);
				if(empty($order_no) && empty($old_order_no)){
					$trans->rollback();
					alert_json(0,"没有订单号！");
				}
				if(!empty($order_no) && !empty($old_order_no)){
					$order_foreachcount++;
				}
				if(empty($order_no) && !empty($old_order_no)){
					$order_no=$old_order_no;
				}
				//流水订单号-begin
				//流水单号兼容事务提交-begin
				// dump($serialNumber);
				$len=strlen($serialNumber_first);
				$ser_nums=intval($serialNumber_first);
				$int_len=strlen($ser_nums);
				$ser_nums=$ser_nums+$order_foreachcount;
				$str_nums="";
				for($j=0;$j<$len-$int_len;$j++){
					$str_nums.='0';
				}
				$serialNumber=$str_nums.$ser_nums;
				//流水单号兼容事务提交-end
				$serialNumber='P'.date("Ymd",time()).$serialNumber;
				//流水订单号
				
				$data['warehouse_code']=I('warehouse_code');
				$data['order_no']=$serialNumber;//流水单号
				$data['old_order_no']=$order_no;//原单号
				if($order_no!=$old_order_no){//不重复
					//重复判断--beging
					$m_info=$m->where(array('old_order_no'=>$order_no))->find();
					// echo $m->getlastsql();
					if(!empty($m_info)){
						$return['status']=1;
						$return['message']="要导入的订单编号已经存在！";
						$this->ajaxReturn($return);
					}
					//重复判断-end
				}
				$data['buyer_name']		= $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
				$data['buyer_phone']		= $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
				
				$data['user_name']		= $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
				$data['user_phone']			= $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
				$data['province']			= $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
				$data['city']	= $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				$data['area']	= $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
				$data['user_address']			= $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
				if($data['user_address'] instanceof \PHPExcel_RichText) { //富文本转换字符串
                    $data['user_address'] = $data['user_address']->__toString();
                }
				
				$data['add_time']		= time();

				$data_og['bar_code']	= $objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
				$data_og['goods_nums']	= $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
				$data_og['goods_price']	= $objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();
				$data['total_price']	= $objPHPExcel->getActiveSheet()->getCell("K".$i)->getCalculatedValue();
				
				$data['platform_name']	= $objPHPExcel->getActiveSheet()->getCell("L".$i)->getValue();
				$transport	= $objPHPExcel->getActiveSheet()->getCell("M".$i)->getValue();//运输方式
				if(!empty($transport)){
					$data['transport_cate']=$transport;
				}
				$data['erp_mark_mailno']	= $objPHPExcel->getActiveSheet()->getCell("N".$i)->getValue();//备注
				if($data['erp_mark_mailno'] instanceof \PHPExcel_RichText) { //富文本转换字符串
                    $data['erp_mark_mailno'] = $data['erp_mark_mailno']->__toString();
                }
				
				$print_order['receiveMan']=$objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
				$print_order['receiveManPhone']=$objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
				$print_order['receiveProvince']=$objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
				$print_order['receiveCity']=$objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				$print_order['receiveCounty']=$objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
				$print_order['receiveManAddress']=$objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
				if($print_order['receiveManAddress'] instanceof \PHPExcel_RichText) { //富文本转换字符串
                    $print_order['receiveManAddress'] = $print_order['receiveManAddress']->__toString();
                }
				
				$print_order['txLogisticID']=$objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
				$print_order['order_no']=$serialNumber;
				
				$print_order['add_time']=time();//添加时间
				if(empty($data_og['bar_code'])){
					$trans->rollback();
					$return['status']=0;
					$return['message']="请核对数据，并删除空行！";
					$this->ajaxReturn($return);
				}
				if(empty($data['total_price'])){
					$trans->rollback();
					$return['status']=0;
					$return['message']="总价不能为空！";
					$this->ajaxReturn($return);
				}
				$data_og['goods_no']	=$merchant_code.$data_og['bar_code'];//商品编码+条形码
				$pros_map =array();
				$pros_map['goods_no']=$data_og['goods_no'];
				$goods_info=$pro->where($pros_map)->find();
				
				if(empty($goods_info)){
					$trans->rollback();
					$return['status']=0;
					$return['message']="账册中没有查到对应条码信息【".$data_og['bar_code']."】";
					$this->ajaxReturn($return);
				}else{
					if(empty($data_og['goods_price'])){
						if(empty($goods_info['price'])){
							$trans->rollback();
							$return['status']=0;
							$return['message']="账册中没有查到对应条码信息【".$data_og['bar_code']."】对应的单价，请完善信息后再导入";
							$this->ajaxReturn($return);
						}
					}
				}
				if(empty($data_og['goods_nums'])){
					$trans->rollback();
					$return['status']=0;
					$return['message']= $i."行 共".$highestRow."行 商品数量不能为空";
					$this->ajaxReturn($return);
				}
				if($order_no!=$old_order_no){//不重复
					//添加订单
					$insert_id=$m->add($data);
					if(!$insert_id){
						$trans->rollback();
					}
					//添加订单详情
					$data_og['order_id']=$insert_id;
					$res_gg=$g->add($data_og);
					if(!$res_gg){
						$trans->rollback();
					}
					//添加面单
					$res_p=$p->add($print_order);
					if(!$res_p){
						$trans->rollback();
					}
				}else{//重复
					//添加订单详情
					$data_og['order_id']=$insert_id;
					$res_gg=$g->add($data_og);
					if(!$res_gg){
						$trans->rollback();
						alert_json(0,'订单重复');
					}
				}
				$old_order_no=$order_no;
				$push_data['detail'][$i-2]['goods_detail_id']=$res_gg;//商品详细id
				$push_data['detail'][$i-2]['goods_nums']=$data_og['goods_nums'];//数量
				$push_data['detail'][$i-2]['goods_no']=$data_og['goods_no'];//商品编码
			}
			// dump($push_data);
			// die;
			//扣库存-begin
			// $push_data['warehouse_code']='12345678';//仓库编码
			$push_data['warehouse_code']=I('warehouse_code');//一般仓仓库编码
			// $push_data['goods_nums']=$data_og['goods_nums'];//数量
			// $push_data['goods_no']=$data_og['goods_no'];//商品编码
			$url=C('base_config.freez_storage');
			if(empty($push_data['warehouse_code'])){
				alert_json(0,'没有传入仓库编码');
			}
			if(empty($url)){
				alert_json(0,'请配置冻结库存接口');
			}
			$res_edit=push($url,$push_data);
			$res_edit=json_decode($res_edit,true);
			file_put_contents('./log/freez_storage.log',date('Y-m-d H:i:s').var_export($res_edit,true).PHP_EOL,FILE_APPEND);//日志导出sql
			if(!$res_edit['status']){
				$trans->rollback();
				alert_json(0,$res_edit['message']);
			}else{
				foreach($res_edit['detail'] as $kss=>$vss){
					$map_p['id']=$vss['goods_detail_id'];//订单明细对应商品id
					$data_p['goods_batch_no']=$vss['str'];//关联批次和数量
					// $data_p['goods_batch_no']=array('exp',"concat(goods_batch_no,',$str')");//关联批次和数量
					$g->where($map_p)->save($data_p);
				}
			}
			//扣库存-end
			$trans->commit();
			$return['status']=1;
			$return['message']="导入成功！";
			$this->ajaxReturn($return);
		}else
		{
			$return['status']=0;
			$return['message']="请选择上传的文件!";
			$this->ajaxReturn($return);
		}
	}
	//导入订单-（一般正常模板）
	public function doimportPushorder_normal_All(){
		$param = I('param.','','trim');
		if (!empty($_FILES)) {
			$config = array(
				'maxSize'    =>    3145728,    
				'savePath'   =>    '/Import/',
				'saveName'   =>    array('uniqid',''),    
				'exts'       =>    array('xlsx', 'xls'),    
				'autoSub'    =>    true,    
				'subName'    =>    array('date','Ymd'),);
			$upload = new \Think\Upload($config);// 实例化上传类
			$info   =   $upload->upload();    
			if(!$info) {// 上传错误提示错误信息      
				$return['status']=0;
				$return['message']=$upload->getError();
				$this->ajaxReturn($return);    
			}
			$extension=$info['import']['ext'];
			// vendor("PHPExcel.PHPExcel");
			//导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
			import("Org.Util.PHPExcel");
			import("Org.Util.PHPExcel.Writer.Excel5");
			import("Org.Util.PHPExcel.Writer.Excel2007");
			import("Org.Util.PHPExcel.IOFactory.php");
			
			//创建PHPExcel对象，注意，不能少了\
			$objPHPExcel = new \PHPExcel();
			
			$file_name='./Uploads'.$info['import']['savepath'].$info['import']['savename'];
			// dump($file_name);
			// die;
			if($extension=='xlsx'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
			}
			if($extension=='xls'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel5');
			}
			$objPHPExcel = $objReader->load($file_name,$encode='utf-8');
			$sheet = $objPHPExcel->getSheet(0);
			$highestRow = $sheet->getHighestRow(); // 取得总行数
			$highestColumn = $sheet->getHighestColumn(); // 取得总列数
			// $sql = 'truncate table '.$db_name.'.'.C('DB_PREFIX_T').'arrival_order';
			// $stat=M()->execute($sql);//清空表
			$outbound=A('Outbound');
			$warehouse_code=$param['warehouse_code'];
			
			$order_foreachcount=0;
			$trans_arr=array();//事务变量名数组
			for($i=2;$i<=$highestRow;$i++)
			{
				$company_name=$objPHPExcel->getActiveSheet()->getCell("O".$i)->getValue();
				if(empty($company_name)){
					foreach($trans_arr as $k=>$v){
						$$v->rollback();
					}
					alert_json(0,$highestRow.'行的货主名称不能为空！');
				}
				$username=$this->user($company_name);
				$username_trans='trans_'.$username;//可变变量
				if(empty($$username_trans)){
					$trans_arr[]=$username_trans;
					$$username_trans = M('',C('DB_PREFIX_T'), C('db_config'));
					$$username_trans->startTrans();   // 开启事务
				}
				
				$m=M('Pushorder',C('DB_PREFIX_T'), C('db_config'));
				$g=M('Pushordergoods',C('DB_PREFIX_T'), C('db_config'));
				$p=M('Printorder',C('DB_PREFIX_T'), C('db_config'));
				$pro=M('Product',C('DB_PREFIX_T'), C('db_config'));
				// $region=M('Region');
				//商家编码--begin
					$merchant_code=C('base_config.merchant_code');//商家编码
				//商家编码--end
				$serialNumber_first=$outbound->serialNumber('Pushorder',$username,$warehouse_code);
				
				$data=array();
				$data_og=array();
				$print_order=array();
				$order_no		= $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();//订单编号
				// dump($order_no);dump($old_order_no);
				if(empty($order_no) && empty($old_order_no)){
					foreach($trans_arr as $k=>$v){
						$$v->rollback();
					}
					alert_json(0,"没有订单号！");
				}
				if(!empty($order_no) && !empty($old_order_no)){
					$order_foreachcount++;
				}
				if(empty($order_no) && !empty($old_order_no)){
					$order_no=$old_order_no;
				}
				//流水订单号-begin
				//流水单号兼容事务提交-begin
				// dump($serialNumber);
				$len=strlen($serialNumber_first);
				$ser_nums=intval($serialNumber_first);
				$int_len=strlen($ser_nums);
				$ser_nums=$ser_nums+$order_foreachcount;
				$str_nums="";
				for($j=0;$j<$len-$int_len;$j++){
					$str_nums.='0';
				}
				$serialNumber=$str_nums.$ser_nums;
				//流水单号兼容事务提交-end
				$serialNumber='P'.date("Ymd",time()).$serialNumber;
				//流水订单号
				
				$data['warehouse_code']=I('warehouse_code');
				$data['order_no']=$serialNumber;//流水单号
				$data['old_order_no']=$order_no;//原单号
				if($order_no!=$old_order_no){//不重复
					//重复判断--beging
					$m_info=$m->where(array('old_order_no'=>$order_no))->find();
					// echo $m->getlastsql();
					if(!empty($m_info)){
						$return['status']=1;
						$return['message']="要导入的订单编号已经存在！";
						$this->ajaxReturn($return);
					}
					//重复判断-end
				}
				$data['buyer_name']		= $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
				$data['buyer_phone']		= $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
				
				$data['user_name']		= $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
				$data['user_phone']			= $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
				$data['province']			= $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
				$data['city']	= $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				$data['area']	= $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
				$data['user_address']			= $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
				if($data['user_address'] instanceof \PHPExcel_RichText) { //富文本转换字符串
                    $data['user_address'] = $data['user_address']->__toString();
                }
				
				$data['add_time']		= time();

				$data_og['bar_code']	= $objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
				$data_og['goods_nums']	= $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
				$data_og['goods_price']	= $objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();
				$data['total_price']	= $objPHPExcel->getActiveSheet()->getCell("K".$i)->getCalculatedValue();
				
				$data['platform_name']	= $objPHPExcel->getActiveSheet()->getCell("L".$i)->getValue();
				$transport	= $objPHPExcel->getActiveSheet()->getCell("M".$i)->getValue();//运输方式
				if(!empty($transport)){
					$data['transport_cate']=$transport;
				}
				$data['erp_mark_mailno']	= $objPHPExcel->getActiveSheet()->getCell("N".$i)->getValue();//备注
				if($data['erp_mark_mailno'] instanceof \PHPExcel_RichText) { //富文本转换字符串
                    $data['erp_mark_mailno'] = $data['erp_mark_mailno']->__toString();
                }
				
				$print_order['receiveMan']=$objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
				$print_order['receiveManPhone']=$objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
				$print_order['receiveProvince']=$objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
				$print_order['receiveCity']=$objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				$print_order['receiveCounty']=$objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
				$print_order['receiveManAddress']=$objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
				if($print_order['receiveManAddress'] instanceof \PHPExcel_RichText) { //富文本转换字符串
                    $print_order['receiveManAddress'] = $print_order['receiveManAddress']->__toString();
                }
				
				$print_order['txLogisticID']=$objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
				$print_order['order_no']=$serialNumber;
				
				$print_order['add_time']=time();//添加时间
				if(empty($data_og['bar_code'])){
					foreach($trans_arr as $k=>$v){
						$$v->rollback();
					}
					$return['status']=0;
					$return['message']="请核对数据，并删除空行！";
					$this->ajaxReturn($return);
				}
				if(empty($data['total_price'])){
					foreach($trans_arr as $k=>$v){
						$$v->rollback();
					}
					$return['status']=0;
					$return['message']="总价不能为空！";
					$this->ajaxReturn($return);
				}
				$data_og['goods_no']	=$merchant_code.$data_og['bar_code'];//商品编码+条形码
				$pros_map =array();
				$pros_map['goods_no']=$data_og['goods_no'];
				$goods_info=$pro->where($pros_map)->find();
				// dump(123);
				// echo $pro->getlastsql();
				// die;
				if(empty($goods_info)){
					foreach($trans_arr as $k=>$v){
						$$v->rollback();
					}
					$return['status']=0;
					$return['message']="账册中没有查到对应条码信息【".$data_og['bar_code']."】";
					$this->ajaxReturn($return);
				}else{
					if(empty($data_og['goods_price'])){
						if(empty($goods_info['price'])){
							foreach($trans_arr as $k=>$v){
								$$v->rollback();
							}
							$return['status']=0;
							$return['message']="账册中没有查到对应条码信息【".$data_og['bar_code']."】对应的单价，请完善信息后再导入";
							$this->ajaxReturn($return);
						}
					}
				}
				if(empty($data_og['goods_nums'])){
					foreach($trans_arr as $k=>$v){
						$$v->rollback();
					}
					$return['status']=0;
					$return['message']= $i."行 共".$highestRow."行 商品数量不能为空";
					$this->ajaxReturn($return);
				}
				if($order_no!=$old_order_no){//不重复
					//添加订单
					$insert_id=$m->add($data);
					if(!$insert_id){
						foreach($trans_arr as $k=>$v){
							$$v->rollback();
						}
					}
					//添加订单详情
					$data_og['order_id']=$insert_id;
					$res_gg=$g->add($data_og);
					if(!$res_gg){
						foreach($trans_arr as $k=>$v){
							$$v->rollback();
						}
					}
					//添加面单
					$res_p=$p->add($print_order);
					if(!$res_p){
						foreach($trans_arr as $k=>$v){
							$$v->rollback();
						}
					}
				}else{//重复
					//添加订单详情
					$data_og['order_id']=$insert_id;
					$res_gg=$g->add($data_og);
					if(!$res_gg){
						foreach($trans_arr as $k=>$v){
							$$v->rollback();
							alert_json(0,'订单重复');
						}
					}
				}
				$old_order_no=$order_no;
				$push_data['detail'][$i-2]['goods_detail_id']=$res_gg;//商品详细id
				$push_data['detail'][$i-2]['goods_nums']=$data_og['goods_nums'];//数量
				$push_data['detail'][$i-2]['goods_no']=$data_og['goods_no'];//商品编码
			}
			// dump($push_data);
			// die;
			//扣库存-begin
			// $push_data['warehouse_code']='12345678';//仓库编码
			$push_data['warehouse_code']=I('warehouse_code');//一般仓仓库编码
			// $push_data['goods_nums']=$data_og['goods_nums'];//数量
			// $push_data['goods_no']=$data_og['goods_no'];//商品编码
			$url=C('base_config.freez_storage');
			if(empty($push_data['warehouse_code'])){
				alert_json(0,'没有传入仓库编码');
			}
			if(empty($url)){
				alert_json(0,'请配置冻结库存接口');
			}
			$res_edit=push($url,$push_data);
			$res_edit=json_decode($res_edit,true);
			file_put_contents('./log/freez_storage.log',date('Y-m-d H:i:s').var_export($res_edit,true).PHP_EOL,FILE_APPEND);//日志导出sql
			if(!$res_edit['status']){
				foreach($trans_arr as $k=>$v){
					$$v->rollback();
				}
				alert_json(0,$res_edit['message']);
			}else{
				foreach($res_edit['detail'] as $kss=>$vss){
					$map_p['id']=$vss['goods_detail_id'];//订单明细对应商品id
					$data_p['goods_batch_no']=$vss['str'];//关联批次和数量
					// $data_p['goods_batch_no']=array('exp',"concat(goods_batch_no,',$str')");//关联批次和数量
					$g->where($map_p)->save($data_p);
				}
			}
			//扣库存-end
			foreach($trans_arr as $k=>$v){
				$$v->commit();
			}
			$return['status']=1;
			$return['message']="导入成功！";
			$this->ajaxReturn($return);
		}else
		{
			$return['status']=0;
			$return['message']="请选择上传的文件!";
			$this->ajaxReturn($return);
		}
	}
	//导入订单-(跨境正常模板)
	public function doimportPushorder(){
		$param = I('param.','','trim');
		$owner_status=session('agent.owner_status');//货主属性0:否1是
		if(!$owner_status){//不是货主（主erp）
			$order=A('order');
			$order->is_owner($param['username'],$param['warehouse_code']);
		}
		$trans = M('',C('DB_PREFIX_T'), C('db_config'));
        $trans->startTrans();   // 开启事务
		$m=M('Pushorder',C('DB_PREFIX_T'), C('db_config'));
		$g=M('Pushordergoods',C('DB_PREFIX_T'), C('db_config'));
		$p=M('Printorder',C('DB_PREFIX_T'), C('db_config'));
		$pro=M('Product',C('DB_PREFIX_T'), C('db_config'));
		// $region=M('Region');
		//商家编码--begin
			$merchant_code=C('base_config.merchant_code');//商家编码
		//商家编码--end
		if (!empty($_FILES)) {
			$config = array(
				'maxSize'    =>    3145728,    
				'savePath'   =>    '/Import/',
				'saveName'   =>    array('uniqid',''),    
				'exts'       =>    array('xlsx', 'xls'),    
				'autoSub'    =>    true,    
				'subName'    =>    array('date','Ymd'),);
			$upload = new \Think\Upload($config);// 实例化上传类
			$info   =   $upload->upload();    
			if(!$info) {// 上传错误提示错误信息      
				$return['status']=0;
				$return['message']=$upload->getError();
				$this->ajaxReturn($return);    
			}
			$extension=$info['import']['ext'];
			// vendor("PHPExcel.PHPExcel");
			//导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
			import("Org.Util.PHPExcel");
			import("Org.Util.PHPExcel.Writer.Excel5");
			import("Org.Util.PHPExcel.Writer.Excel2007");
			import("Org.Util.PHPExcel.IOFactory.php");
			
			//创建PHPExcel对象，注意，不能少了\
			$objPHPExcel = new \PHPExcel();
			
			$file_name='./Uploads'.$info['import']['savepath'].$info['import']['savename'];
			// dump($file_name);
			// die;
			if($extension=='xlsx'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
			}
			if($extension=='xls'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel5');
			}
			$objPHPExcel = $objReader->load($file_name,$encode='utf-8');
			$sheet = $objPHPExcel->getSheet(0);
			$highestRow = $sheet->getHighestRow(); // 取得总行数
			$highestColumn = $sheet->getHighestColumn(); // 取得总列数
			// $sql = 'truncate table '.$db_name.'.'.C('DB_PREFIX_T').'arrival_order';
			// $stat=M()->execute($sql);//清空表
			$outbound=A('Outbound');
			$username=$param['username'];
			$warehouse_code=$param['warehouse_code'];
			$serialNumber_first=$outbound->serialNumber('Pushorder',$username,$warehouse_code);
			$order_foreachcount=0;
			for($i=2;$i<=$highestRow;$i++)
			{
				$data=array();
				$data_og=array();
				$print_order=array();
				$order_no		= $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();//订单编号
				// dump($order_no);dump($old_order_no);
				if(empty($order_no) && empty($old_order_no)){
					$trans->rollback();
					alert_json(0,"没有订单号！");
				}
				if(!empty($order_no) && !empty($old_order_no)){
					$order_foreachcount++;
				}
				if(empty($order_no) && !empty($old_order_no)){
					$order_no=$old_order_no;
				}
				//流水订单号-begin
				//流水单号兼容事务提交-begin
				// dump($serialNumber);
				$len=strlen($serialNumber_first);
				$ser_nums=intval($serialNumber_first);
				$int_len=strlen($ser_nums);
				$ser_nums=$ser_nums+$order_foreachcount;
				$str_nums="";
				for($j=0;$j<$len-$int_len;$j++){
					$str_nums.='0';
				}
				$serialNumber=$str_nums.$ser_nums;
				//流水单号兼容事务提交-end
				$serialNumber='P'.date("Ymd",time()).$serialNumber;
				//流水订单号
				
				$data['warehouse_code']=I('warehouse_code');
				$data['order_no']=$serialNumber;//流水单号
				$data['old_order_no']=$order_no;//原单号
				if($order_no!=$old_order_no){//不重复
					//重复判断--beging
					$m_info=$m->where(array('old_order_no'=>$order_no))->find();
					if(!empty($m_info)){
						$return['status']=1;
						$return['message']="要导入的订单编号已经存在！";
						$this->ajaxReturn($return);
					}
					//重复判断-end
				}
				$data['buyer_name']		= $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
				$data['buyer_phone']		= $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
				$data['buyer_idcard']		= $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
				
				$data['user_name']		= $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				$data['user_phone']			= $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
				$data['province']			= $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
				$data['city']	= $objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
				$data['area']	= $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
				$data['user_address']			= $objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();
				if($data['user_address'] instanceof \PHPExcel_RichText) { //富文本转换字符串
                    $data['user_address'] = $data['user_address']->__toString();
                }
				
				$data['add_time']		= time();

				$data_og['bar_code']	= $objPHPExcel->getActiveSheet()->getCell("K".$i)->getValue();
				// $data_og['goods_name']	= $objPHPExcel->getActiveSheet()->getCell("Q".$i)->getValue();
				$data_og['goods_nums']	= $objPHPExcel->getActiveSheet()->getCell("L".$i)->getValue();
				$data_og['goods_price']	= $objPHPExcel->getActiveSheet()->getCell("M".$i)->getValue();
				$data['platform_name']	= $objPHPExcel->getActiveSheet()->getCell("N".$i)->getValue();
				$data['erp_mark_mailno']	= $objPHPExcel->getActiveSheet()->getCell("O".$i)->getValue();//备注
				if($data['erp_mark_mailno'] instanceof \PHPExcel_RichText) { //富文本转换字符串
                    $data['erp_mark_mailno'] = $data['erp_mark_mailno']->__toString();
                }

				$print_order['receiveMan']=$objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				$print_order['receiveManPhone']=$objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
				$print_order['receiveProvince']=$objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
				$print_order['receiveCity']=$objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
				$print_order['receiveCounty']=$objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
				$print_order['receiveManAddress']=$objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();
				if($print_order['receiveManAddress'] instanceof \PHPExcel_RichText) { //富文本转换字符串
                    $print_order['receiveManAddress'] = $print_order['receiveManAddress']->__toString();
                }

				$print_order['txLogisticID']=$objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
				$print_order['order_no']=$serialNumber;
				
				$print_order['add_time']=time();//添加时间
				if(empty($data_og['bar_code'])){
					$trans->rollback();
					$return['status']=0;
					$return['message']="请核对数据，并删除空行！";
					$this->ajaxReturn($return);
				}
				$data_og['goods_no']	=$merchant_code.$data_og['bar_code'];//商品编码+
				$pros_map =array();
				$pros_map['goods_no']=$data_og['goods_no'];
				$goods_info=$pro->where($pros_map)->find();

				if(empty($goods_info)){
					$trans->rollback();
					$return['status']=0;
					$return['message']="账册中没有查到对应条码信息【".$data_og['bar_code']."】";
					$this->ajaxReturn($return);
				}else{
					if(empty($goods_info['pn'])){
						$trans->rollback();
						$return['status']=0;
						$return['message']="账册中没有查到对应条码信息【".$data_og['bar_code']."】对应的料号信息，请完善信息后再导入";
						$this->ajaxReturn($return);
					}
					if(empty($data_og['goods_price'])){
						if(empty($goods_info['price'])){
							$trans->rollback();
							$return['status']=0;
							$return['message']="账册中没有查到对应条码信息【".$data_og['bar_code']."】对应的单价，请完善信息后再导入";
							$this->ajaxReturn($return);
						}
					}
					if(empty($goods_info['net_weight'])){
						$trans->rollback();
						$return['status']=0;
						$return['message']="账册中没有查到对应条码信息【".$data_og['bar_code']."】对应的净重，请完善信息后再导入";
						$this->ajaxReturn($return);
					}
					if(empty($goods_info['gross_weight'])){
						$trans->rollback();
						$return['status']=0;
						$return['message']="账册中没有查到对应条码信息【".$data_og['bar_code']."】对应的毛重，请完善信息后再导入";
						$this->ajaxReturn($return);
					}
					if(empty($goods_info['goods_tax'])){
						$trans->rollback();
						$return['status']=0;
						$return['message']="账册中没有查到对应条码信息【".$data_og['bar_code']."】对应的税率，请完善信息后再导入";
						$this->ajaxReturn($return);
					}
				}
				if(empty($data_og['goods_nums'])){
					$trans->rollback();
					$return['status']=0;
					$return['message']= $i."行 共".$highestRow."行 商品数量不能为空";
					$this->ajaxReturn($return);
				}
				if($order_no!=$old_order_no){//不重复
					//添加订单
					$insert_id=$m->add($data);
					if(!$insert_id){
						$trans->rollback();
					}
					//添加订单详情
					$data_og['order_id']=$insert_id;
					$res_gg=$g->add($data_og);
					if(!$res_gg){
						$trans->rollback();
					}
					//添加面单
					$res_p=$p->add($print_order);
					if(!$res_p){
						$trans->rollback();
					}
				}else{//重复
					//添加订单详情
					$data_og['order_id']=$insert_id;
					$res_gg=$g->add($data_og);
					if(!$res_gg){
						$trans->rollback();
						alert_json(0,'订单重复');
					}
				}
				$old_order_no=$order_no;
				$push_data['detail'][$i-2]['goods_detail_id']=$res_gg;//商品详细id
				$push_data['detail'][$i-2]['goods_nums']=$data_og['goods_nums'];//数量
				$push_data['detail'][$i-2]['goods_no']=$data_og['goods_no'];//商品编码
			}
			// dump($push_data);
			// die;
			//扣库存-begin
			// $push_data['warehouse_code']='12345678';//仓库编码
			// $push_data['warehouse_code']='11330213';//仓库编码(下沙仓)
			$push_data['warehouse_code']=I('warehouse_code');
			// $push_data['goods_nums']=$data_og['goods_nums'];//数量
			// $push_data['goods_no']=$data_og['goods_no'];//商品编码
			$url=C('base_config.freez_storage');
			if(empty($url)){
				alert_json(0,'请配置冻结库存接口');
			}
			$res_edit=push($url,$push_data);
			$res_edit=json_decode($res_edit,true);
			file_put_contents('./log/freez_storage.log',date('Y-m-d H:i:s').var_export($res_edit,true).PHP_EOL,FILE_APPEND);//日志导出sql
			if(!$res_edit['status']){
				$trans->rollback();
				alert_json(0,$res_edit['message']);
			}else{
				foreach($res_edit['detail'] as $kss=>$vss){
					$map_p['id']=$vss['goods_detail_id'];//订单明细对应商品id
					$data_p['goods_batch_no']=$vss['str'];//关联批次和数量
					// $data_p['goods_batch_no']=array('exp',"concat(goods_batch_no,',$str')");//关联批次和数量
					$g->where($map_p)->save($data_p);
				}
			}
			//扣库存-end
			$trans->commit();
			$return['status']=1;
			$return['message']="导入成功！";
			$this->ajaxReturn($return);
		}else
		{
			$return['status']=0;
			$return['message']="请选择上传的文件!";
			$this->ajaxReturn($return);
		}
	}
	//公司名称所属erp判断
	public function user($company_name){
		$u=M('User');
		$u_list=$u->where(array('company'=>trim($company_name)))->find();
		$username=$u_list['username'];
		$db_name=C('DB_PREFIX_T').$username;
		C('db_config.DB_NAME',$db_name);//当前
		$base_config=M('Config',C('DB_PREFIX_T'), C('db_config'));
		$list=$base_config->getField('en_name,value');
		C('base_config',$list);
		return $username;
	}
	//导入全部订单-(跨境正常模板)
	public function doimportPushorder_All(){
		$param = I('param.','','trim');
		if (!empty($_FILES)) {
			$config = array(
				'maxSize'    =>    3145728,    
				'savePath'   =>    '/Import/',
				'saveName'   =>    array('uniqid',''),    
				'exts'       =>    array('xlsx', 'xls'),    
				'autoSub'    =>    true,    
				'subName'    =>    array('date','Ymd'),);
			$upload = new \Think\Upload($config);// 实例化上传类
			$info   =   $upload->upload();    
			if(!$info) {// 上传错误提示错误信息      
				$return['status']=0;
				$return['message']=$upload->getError();
				$this->ajaxReturn($return);    
			}
			$extension=$info['import']['ext'];
			// vendor("PHPExcel.PHPExcel");
			//导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
			import("Org.Util.PHPExcel");
			import("Org.Util.PHPExcel.Writer.Excel5");
			import("Org.Util.PHPExcel.Writer.Excel2007");
			import("Org.Util.PHPExcel.IOFactory.php");
			
			//创建PHPExcel对象，注意，不能少了\
			$objPHPExcel = new \PHPExcel();
			
			$file_name='./Uploads'.$info['import']['savepath'].$info['import']['savename'];
			// dump($file_name);
			// die;
			if($extension=='xlsx'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
			}
			if($extension=='xls'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel5');
			}
			$objPHPExcel = $objReader->load($file_name,$encode='utf-8');
			$sheet = $objPHPExcel->getSheet(0);
			$highestRow = $sheet->getHighestRow(); // 取得总行数
			$highestColumn = $sheet->getHighestColumn(); // 取得总列数
			// $sql = 'truncate table '.$db_name.'.'.C('DB_PREFIX_T').'arrival_order';
			// $stat=M()->execute($sql);//清空表
			$outbound=A('Outbound');
			$warehouse_code=$param['warehouse_code'];
			// dump($highestRow);
			// die;
			$order_foreachcount=0;
			$trans_arr=array();//事务变量名数组
			for($i=2;$i<=$highestRow;$i++)
			{
				$company_name=$objPHPExcel->getActiveSheet()->getCell("P".$i)->getValue();
				if(empty($company_name)){
					foreach($trans_arr as $k=>$v){
						$$v->rollback();
					}
					alert_json(0,$highestRow.'行的货主名称不能为空！');
				}
				$username=$this->user($company_name);
				$username_trans='trans_'.$username;//可变变量
				if(empty($$username_trans)){
					$trans_arr[]=$username_trans;
					$$username_trans = M('',C('DB_PREFIX_T'), C('db_config'));
					$$username_trans->startTrans();   // 开启事务
				}
				
				$m=M('Pushorder',C('DB_PREFIX_T'), C('db_config'));
				$g=M('Pushordergoods',C('DB_PREFIX_T'), C('db_config'));
				$p=M('Printorder',C('DB_PREFIX_T'), C('db_config'));
				$pro=M('Product',C('DB_PREFIX_T'), C('db_config'));
				// $region=M('Region');
				//商家编码--begin
					$merchant_code=C('base_config.merchant_code');//商家编码
				//商家编码--end
				$serialNumber_first=$outbound->serialNumber('Pushorder',$username,$warehouse_code);
				
				$data=array();
				$data_og=array();
				$print_order=array();
				$order_no		= $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();//订单编号
				// dump($order_no);dump($old_order_no);
				if(empty($order_no) && empty($old_order_no)){
					foreach($trans_arr as $k=>$v){
						$$v->rollback();
					}
					alert_json(0,"没有订单号！");
				}
				if(!empty($order_no) && !empty($old_order_no)){
					$order_foreachcount++;
				}
				if(empty($order_no) && !empty($old_order_no)){
					$order_no=$old_order_no;
				}
				//流水订单号-begin
				//流水单号兼容事务提交-begin
				// dump($serialNumber);
				$len=strlen($serialNumber_first);
				$ser_nums=intval($serialNumber_first);
				$int_len=strlen($ser_nums);
				$ser_nums=$ser_nums+$order_foreachcount;
				$str_nums="";
				for($j=0;$j<$len-$int_len;$j++){
					$str_nums.='0';
				}
				$serialNumber=$str_nums.$ser_nums;
				//流水单号兼容事务提交-end
				$serialNumber='P'.date("Ymd",time()).$serialNumber;
				//流水订单号
				
				$data['warehouse_code']=I('warehouse_code');
				$data['order_no']=$serialNumber;//流水单号
				$data['old_order_no']=$order_no;//原单号
				if($order_no!=$old_order_no){//不重复
					//重复判断--beging
					$m_info=$m->where(array('old_order_no'=>$order_no))->find();
					if(!empty($m_info)){
						$return['status']=1;
						$return['message']="要导入的订单编号已经存在！";
						$this->ajaxReturn($return);
					}
					//重复判断-end
				}
				$data['buyer_name']		= $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
				$data['buyer_phone']		= $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
				$data['buyer_idcard']		= $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
				
				$data['user_name']		= $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				$data['user_phone']			= $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
				$data['province']			= $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
				$data['city']	= $objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
				$data['area']	= $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
				$data['user_address']			= $objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();
				if($data['user_address'] instanceof \PHPExcel_RichText) { //富文本转换字符串
                    $data['user_address'] = $data['user_address']->__toString();
                }
				
				$data['add_time']		= time();

				$data_og['bar_code']	= $objPHPExcel->getActiveSheet()->getCell("K".$i)->getValue();
				// $data_og['goods_name']	= $objPHPExcel->getActiveSheet()->getCell("Q".$i)->getValue();
				$data_og['goods_nums']	= $objPHPExcel->getActiveSheet()->getCell("L".$i)->getValue();
				$data_og['goods_price']	= $objPHPExcel->getActiveSheet()->getCell("M".$i)->getValue();
				$data['platform_name']	= $objPHPExcel->getActiveSheet()->getCell("N".$i)->getValue();
				$data['erp_mark_mailno']	= $objPHPExcel->getActiveSheet()->getCell("O".$i)->getValue();//备注
				if($data['erp_mark_mailno'] instanceof \PHPExcel_RichText) { //富文本转换字符串
                    $data['erp_mark_mailno'] = $data['erp_mark_mailno']->__toString();
                }

				$print_order['receiveMan']=$objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				$print_order['receiveManPhone']=$objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
				$print_order['receiveProvince']=$objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
				$print_order['receiveCity']=$objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
				$print_order['receiveCounty']=$objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
				$print_order['receiveManAddress']=$objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();
				if($print_order['receiveManAddress'] instanceof \PHPExcel_RichText) { //富文本转换字符串
                    $print_order['receiveManAddress'] = $print_order['receiveManAddress']->__toString();
                }

				$print_order['txLogisticID']=$objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
				$print_order['order_no']=$serialNumber;
				
				$print_order['add_time']=time();//添加时间
				if(empty($data_og['bar_code'])){
					foreach($trans_arr as $k=>$v){
						$$v->rollback();
					}
					$return['status']=0;
					$return['message']="请核对数据，并删除空行！";
					$this->ajaxReturn($return);
				}
				$data_og['goods_no']	=$merchant_code.$data_og['bar_code'];//商品编码
				$pros_map =array();
				$pros_map['goods_no']=$data_og['goods_no'];
				$goods_info=$pro->where($pros_map)->find();

				if(empty($goods_info)){
					foreach($trans_arr as $k=>$v){
						$$v->rollback();
					}
					$return['status']=0;
					$return['message']="账册中没有查到对应条码信息【".$data_og['bar_code']."】";
					$this->ajaxReturn($return);
				}else{
					if(empty($goods_info['pn'])){
						foreach($trans_arr as $k=>$v){
							$$v->rollback();
						}
						$return['status']=0;
						$return['message']="账册中没有查到对应条码信息【".$data_og['bar_code']."】对应的料号信息，请完善信息后再导入";
						$this->ajaxReturn($return);
					}
					if(empty($data_og['goods_price'])){
						if(empty($goods_info['price'])){
							foreach($trans_arr as $k=>$v){
								$$v->rollback();
							}
							$return['status']=0;
							$return['message']="账册中没有查到对应条码信息【".$data_og['bar_code']."】对应的单价，请完善信息后再导入";
							$this->ajaxReturn($return);
						}
					}
					if(empty($goods_info['net_weight'])){
						foreach($trans_arr as $k=>$v){
							$$v->rollback();
						}
						$return['status']=0;
						$return['message']="账册中没有查到对应条码信息【".$data_og['bar_code']."】对应的净重，请完善信息后再导入";
						$this->ajaxReturn($return);
					}
					if(empty($goods_info['gross_weight'])){
						foreach($trans_arr as $k=>$v){
							$$v->rollback();
						}
						$return['status']=0;
						$return['message']="账册中没有查到对应条码信息【".$data_og['bar_code']."】对应的毛重，请完善信息后再导入";
						$this->ajaxReturn($return);
					}
					if(empty($goods_info['goods_tax'])){
						foreach($trans_arr as $k=>$v){
							$$v->rollback();
						}
						$return['status']=0;
						$return['message']="账册中没有查到对应条码信息【".$data_og['bar_code']."】对应的税率，请完善信息后再导入";
						$this->ajaxReturn($return);
					}
				}
				if(empty($data_og['goods_nums'])){
					foreach($trans_arr as $k=>$v){
						$$v->rollback();
					}
					$return['status']=0;
					$return['message']= $i."行 共".$highestRow."行 商品数量不能为空";
					$this->ajaxReturn($return);
				}
				if($order_no!=$old_order_no){//不重复
					//添加订单
					$insert_id=$m->add($data);
					if(!$insert_id){
						foreach($trans_arr as $k=>$v){
							$$v->rollback();
						}
					}
					//添加订单详情
					$data_og['order_id']=$insert_id;
					$res_gg=$g->add($data_og);
					if(!$res_gg){
						foreach($trans_arr as $k=>$v){
							$$v->rollback();
						}
					}
					//添加面单
					$res_p=$p->add($print_order);
					if(!$res_p){
						foreach($trans_arr as $k=>$v){
							$$v->rollback();
						}
					}
				}else{//重复
					//添加订单详情
					$data_og['order_id']=$insert_id;
					$res_gg=$g->add($data_og);
					if(!$res_gg){
						foreach($trans_arr as $k=>$v){
							$$v->rollback();
							alert_json(0,'订单重复');
						}
					}
				}
				$old_order_no=$order_no;
				$push_data['detail'][$i-2]['goods_detail_id']=$res_gg;//商品详细id
				$push_data['detail'][$i-2]['goods_nums']=$data_og['goods_nums'];//数量
				$push_data['detail'][$i-2]['goods_no']=$data_og['goods_no'];//商品编码
			}
			// dump($push_data);
			// die;
			//扣库存-begin
			// $push_data['warehouse_code']='12345678';//仓库编码
			// $push_data['warehouse_code']='11330213';//仓库编码(下沙仓)
			$push_data['warehouse_code']=I('warehouse_code');
			// $push_data['goods_nums']=$data_og['goods_nums'];//数量
			// $push_data['goods_no']=$data_og['goods_no'];//商品编码
			$url=C('base_config.freez_storage');
			if(empty($url)){
				alert_json(0,'请配置冻结库存接口');
			}
			$res_edit=push($url,$push_data);
			$res_edit=json_decode($res_edit,true);
			file_put_contents('./log/freez_storage.log',date('Y-m-d H:i:s').var_export($res_edit,true).PHP_EOL,FILE_APPEND);//日志导出sql
			
			if(!$res_edit['status']){
				foreach($trans_arr as $k=>$v){
					$$v->rollback();
				}
				alert_json(0,$res_edit['message']);
			}else{
				foreach($res_edit['detail'] as $kss=>$vss){
					$map_p['id']=$vss['goods_detail_id'];//订单明细对应商品id
					$data_p['goods_batch_no']=$vss['str'];//关联批次和数量
					// $data_p['goods_batch_no']=array('exp',"concat(goods_batch_no,',$str')");//关联批次和数量
					$g->where($map_p)->save($data_p);
				}
			}
			//扣库存-end
			foreach($trans_arr as $k=>$v){
				$$v->commit();
			}
			$return['status']=1;
			$return['message']="导入成功！";
			$this->ajaxReturn($return);
		}else
		{
			$return['status']=0;
			$return['message']="请选择上传的文件!";
			$this->ajaxReturn($return);
		}
	}
	public function importgoods(){
        $this->display();
    }
	public function importstoragegoods(){
		$warehouse_code=I('warehouse_code');
		$this->assign('warehouse_code',$warehouse_code);
        $this->display();
    }
	//导入产品
	function doimportstorageGoods(){
		if (!empty($_FILES)){
			$config = array(
				'maxSize'    =>    3145728,    
				'savePath'   =>    '/Import/',
				'saveName'   =>    array('uniqid',''),    
				'exts'       =>    array('xlsx', 'xls'),    
				'autoSub'    =>    true,    
				'subName'    =>    array('date','Ymd'),);
			$upload = new \Think\Upload($config);// 实例化上传类
			$info   =   $upload->upload();    
			if(!$info) {// 上传错误提示错误信息 
				$return['status']=0;
				$return['message']=$upload->getError();
				$this->ajaxReturn($return);    
			}
			$extension=$info['import']['ext'];
			// vendor("PHPExcel.PHPExcel");
			//导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
			import("Org.Util.PHPExcel");
			import("Org.Util.PHPExcel.Writer.Excel5");
			import("Org.Util.PHPExcel.Writer.Excel2007");
			import("Org.Util.PHPExcel.IOFactory.php");
			
			//创建PHPExcel对象，注意，不能少了\
			$objPHPExcel = new \PHPExcel();
			
			$file_name='./Uploads'.$info['import']['savepath'].$info['import']['savename'];
			// dump($file_name);
			// die;
			if($extension=='xlsx'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
			}
			if($extension=='xls'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel5');
			}
			$objPHPExcel = $objReader->load($file_name,$encode='utf-8');
			$sheet = $objPHPExcel->getSheet(0);
			$highestRow = $sheet->getHighestRow(); // 取得总行数
			$highestColumn = $sheet->getHighestColumn(); // 取得总列数
			
			for($i=2;$i<=$highestRow;$i++)
			{
				$data[$i-2]['order_no']		= $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
				$data[$i-2]['batch_no']		= $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
				$data[$i-2]['manual_id']		= $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
				$data[$i-2]['goods_no']		= $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
				$data[$i-2]['goods_name']		= $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				
				$data[$i-2]['goods_spec']		= $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
				$data[$i-2]['unit1']			= $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
				$data[$i-2]['unit2']			= $objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
				$data[$i-2]['storage_amount']	= $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
				// $data[$i-2]['declare_amount']	= $objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();
				$data[$i-2]['receive_amount']	= $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
				$data[$i-2]['price']			= $objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();
				$data[$i-2]['total_price']	= $data[$i-2]['price']*$data[$i-2]['storage_amount'];
				
				$data[$i-2]['country_code']	= $objPHPExcel->getActiveSheet()->getCell("K".$i)->getValue();
				$data[$i-2]['currency']		= $objPHPExcel->getActiveSheet()->getCell("L".$i)->getValue();
				$data[$i-2]['bar_code']		= $objPHPExcel->getActiveSheet()->getCell("M".$i)->getValue();
				$data[$i-2]['conforming_p']	= $objPHPExcel->getActiveSheet()->getCell("N".$i)->getValue();
				$data[$i-2]['nonconforming_p']= $objPHPExcel->getActiveSheet()->getCell("O".$i)->getValue();
				$data[$i-2]['scrap_one']		= $objPHPExcel->getActiveSheet()->getCell("P".$i)->getValue();
				$data[$i-2]['overdue_time']	= $objPHPExcel->getActiveSheet()->getCell("Q".$i)->getValue();
				$data[$i-2]['erp_code']	= $objPHPExcel->getActiveSheet()->getCell("R".$i)->getValue();
			}
			$data_push['warehouse_code']=I('warehouse_code');
			$data_push['rows']=$data;
			$url=C('base_config.importArrivalorder');
			// dump($data_push);
			$res=push($url,$data_push);
			// dump($res);
			$res=json_decode($res,true);
			if($res['status']){
				$return['status']=1;
				$return['message']='导入成功';
				$this->ajaxReturn($return);
			}else{
				$return['status']=0;
				$return['message']=$res['message'];
				$this->ajaxReturn($return);
			}
		}else
		{
			$return['status']=0;
			$return['message']='请选择上传的文件';
			$this->ajaxReturn($return);
		}
	}
	//导入产品
	function doimportGoods(){
		$g=session('agent');
        $db_name=$g['db'];
		$trans=M('', C('DB_PREFIX_T'), C('db_config'));
		$trans->startTrans();   // 开启事务
		$m=M('ArrivalOrder',C('DB_PREFIX_T'), C('db_config'));
		$g=M('Goods', C('DB_PREFIX_T'), C('db_config'));
		$merchant_code=C('base_config.merchant_code');
		if (!empty($_FILES)){
			$config = array(
				'maxSize'    =>    3145728,    
				'savePath'   =>    '/Import/',    
				'saveName'   =>    array('uniqid',''),    
				'exts'       =>    array('xlsx', 'xls'),    
				'autoSub'    =>    true,    
				'subName'    =>    array('date','Ymd'),);
			$upload = new \Think\Upload($config);// 实例化上传类
			$info   =   $upload->upload();    
			if(!$info) {// 上传错误提示错误信息 
				$return['status']=0;
				$return['message']=$upload->getError();
				$this->ajaxReturn($return);    
			}
			$extension=$info['import']['ext'];
			// vendor("PHPExcel.PHPExcel");
			//导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
			import("Org.Util.PHPExcel");
			import("Org.Util.PHPExcel.Writer.Excel5");
			import("Org.Util.PHPExcel.Writer.Excel2007");
			import("Org.Util.PHPExcel.IOFactory.php");
			
			//创建PHPExcel对象，注意，不能少了\
			$objPHPExcel = new \PHPExcel();
			
			$file_name='./Uploads'.$info['import']['savepath'].$info['import']['savename'];
			// dump($file_name);
			// die;
			if($extension=='xlsx'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
			}
			if($extension=='xls'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel5');
			}
			$objPHPExcel = $objReader->load($file_name,$encode='utf-8');
			$sheet = $objPHPExcel->getSheet(0);
			$highestRow = $sheet->getHighestRow(); // 取得总行数
			$highestColumn = $sheet->getHighestColumn(); // 取得总列数
			
			for($i=2;$i<=$highestRow;$i++)
			{
				$data=array();
				$data['goods_name']		= (string)$objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
				$data['bar_code']		= (string)$objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
				$data['goods_no']=$merchant_code.$data['bar_code'];//商品编码=条形码
				$data['goods_brand']		= (string)$objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
				$data['goods_marque']		= (string)$objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
				$data['price']		= (string)$objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				$data['receive_amount']		= (string)$objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
				$data['goods_tax']		= (string)$objPHPExcel->getActiveSheet()->getCell("G".$i)->getCalculatedValue();
				$data['source_no']		= (string)$objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
				$data['net_weight']		= (string)$objPHPExcel->getActiveSheet()->getCell("I".$i)->getCalculatedValue();
				$data['gross_weight']		= (string)$objPHPExcel->getActiveSheet()->getCell("J".$i)->getCalculatedValue();
				$data['goods_spec']		= (string)$objPHPExcel->getActiveSheet()->getCell("K".$i)->getValue();
				
				$data['storage_amount']=$data['receive_amount'];//库存
				$data['add_time']=time();
				$data['status']=1;
				$data['update_time']=time();
				
				//重复判断
				$map['goods_no']=$data['goods_no'];
				//添加
				$res=$m->add($data);	
				// echo $m->getlastsql();
				if($res){
					$g_info=$g->where($map)->find();//更新库存
					if(empty($g_info)){//没商品信息则新增商品信息
						$data_goods=array();
						$data_goods['bar_code']=$data['bar_code'];
						$data_goods['goods_no']=$data['goods_no'];
						$data_goods['goods_name']=$data['goods_name'];
						$data_goods['goods_brand']=$data['goods_brand'];
						$data_goods['goods_marque']=$data['goods_marque'];
						$data_goods['goods_spec']=$data['goods_spec'];
						// $data_goods['goods_pic']=$data['goods_pic'];
						$data_goods['goods_price']=$data['price'];
						$data_goods['storage_amount']=$data['receive_amount'];
						$data_goods['net_weight']=$data['net_weight'];
						$data_goods['gross_weight']=$data['gross_weight'];
						$res_goods=$g->add($data_goods);
						if(!$res_goods){
							$return['message']='添加库存信息失败';
							$return['status']=false;
							$this->ajaxReturn($return);
						}
					}else{//有商品信息则更新库存
						$order=A('Order');
						$order->edit_storagegoodsnums($map['goods_no'],$data['receive_amount']);
					}
				}else{
					$trans->rollback();
				}
			}
			$trans->commit();
			$return['status']=1;
			$return['message']='导入成功！';
			$this->ajaxReturn($return);
		}else
		{
			$return['status']=0;
			$return['message']='请选择上传的文件';
			$this->ajaxReturn($return);
		}
	}
}